package com.lwj.liwanjia.view.activity.personal;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.lwj.liwanjia.R;
import com.lwj.liwanjia.adapter.ZuJiAdapter;
import com.lwj.liwanjia.base.BaseActivity;
import com.lwj.liwanjia.bean.ZujiBean;
import com.lwj.liwanjia.impl.ZujiView;
import com.lwj.liwanjia.presenter.ZujiPresenter;
import com.lwj.liwanjia.utils.SpfUtils;
import com.lwj.liwanjia.view.activity.login.LoginActivity;
import com.lwj.liwanjia.view.activity.supermarket.ShangpinDetailActivity;
import com.superrecycleview.superlibrary.adapter.SuperBaseAdapter;
import com.superrecycleview.superlibrary.recycleview.SuperRecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by 001 on 2017/9/9.
 */

public class ZuJiActivity extends BaseActivity implements SuperRecyclerView.LoadingListener, ZujiView {

    @BindView(R.id.tv_left)
    TextView tvLeft;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.rlv_zuji)
    SuperRecyclerView rlvZuji;
    @BindView(R.id.progress)
    ProgressBar progress;
    @BindView(R.id.tv_no_jilu)
    TextView tvNojilu;

    private ZuJiAdapter zujiAdapter;//足迹的适配器
    private String token;
    private ZujiPresenter presenter;
    private List<ZujiBean.DataBean> list = new ArrayList<>();

    @Override
    protected void initView() {
        tvLeft.setVisibility(View.VISIBLE);
        tvTitle.setText("我的足迹");
        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        rlvZuji.setLayoutManager(manager);
        rlvZuji.setRefreshEnabled(true);
        rlvZuji.setLoadMoreEnabled(false);
        rlvZuji.setLoadingListener(this);
        zujiAdapter = new ZuJiAdapter(this, list);
        rlvZuji.setAdapter(zujiAdapter);
        token = SpfUtils.getSpfUtils(this).getToken();
        presenter = new ZujiPresenter(this);
        listener();
    }

    private void listener() {
        zujiAdapter.setOnItemClickListener(new SuperBaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, Object item, int position) {
                Intent intent = new Intent(ZuJiActivity.this, ShangpinDetailActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("shopGoodsCode", list.get(position).getCommodityCode());
                bundle.putString("commodityType", list.get(position).getCommodityType());
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
    }

    @Override
    protected void initData() {
        presenter.getZuji(token);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_zu_ji;
    }


    @Override
    public void onRefresh() {
        initData();
        rlvZuji.completeRefresh();
    }

    @Override
    public void onLoadMore() {

    }


    @OnClick(R.id.tv_left)
    public void onViewClicked() {
        finish();
    }

    @Override
    public void showProgress() {
        progress.setVisibility(View.VISIBLE);
    }

    @Override
    public void hideProgress() {
        progress.setVisibility(View.GONE);
    }

    @Override
    public void showError(Exception e) {
        Toast.makeText(this, "访问网络失败", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void getZuji(ZujiBean zujiBean) {
        if (zujiBean.getState() == 0) {
            startActivity(new Intent(this, LoginActivity.class));
            finish();
        } else if (zujiBean.getState() == 1) {
            list.clear();
            list.addAll(zujiBean.getData());
            if(list.size()>0){
                rlvZuji.setVisibility(View.VISIBLE);
                tvNojilu.setVisibility(View.GONE);
                zujiAdapter.notifyDataSetChanged();
            }else{
                rlvZuji.setVisibility(View.GONE);
                tvNojilu.setVisibility(View.VISIBLE);
            }
        }
    }
}
